#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_ReadDTCInfomation_0x19_test.py
@Time    :   2024/09/05 13:31:28
@Author  :   Yangliang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pytest_assume.plugin import assume
 
import pytest
import time
import Set_up
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable
from commDriver.modbus_plc import *

from sqlalchemy import false
sys.dont_write_bytecode = True

SID_0x19_reportNumberOfDTCByStatusMask = [([0x19,0x01,0x01],[0x59,0x01,0x09,0x00,0x00]),# 默认情况下有一个当前DTC code：0x987C54 模块为标定
                                          ([0x19,0x01,0x02],[0x59,0x01,0x09,0x00,0x00]),
                                          ([0x19,0x01,0x04],[0x59,0x01,0x09,0x00,0x00]),
                                          ([0x19,0x01,0x08],[0x59,0x01,0x09,0x00,0x00]),
                                          ([0x19,0x01,0x10],[0x59,0x01,0x09,0x00,0x00]),
                                          ([0x19,0x01,0x20],[0x59,0x01,0x09,0x00,0x00]),
                                          ([0x19,0x01,0x40],[0x59,0x01,0x09,0x00,0x00]),
                                          ([0x19,0x01,0x7F],[0x59,0x01,0x09,0x00,0x00]),# 默认情况下有一个当前DTC code：0x987C54 模块为标定
                                          ([0x19,0x01,0xFF],[0x59,0x01,0x09,0x00,0x00])]# 默认情况下有一个当前DTC code：0x987C54 模块为标定


class Test_ReadDTCInfomation_phy(object):

     ids = ["Request {0}".format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in SID_0x19_reportNumberOfDTCByStatusMask]
     @allure.story('Test reportNumberOfDTCByStatusMask Test')
     @allure.severity('normal')
     @allure.story('Test ReadDTCInfomation Test')
     @allure.description('Test reportDTCByStatusMask of 0x19 in default session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
     @pytest.mark.parametrize('req,resp',SID_0x19_reportNumberOfDTCByStatusMask,ids = ids)
     def test_reportNumberOfDTCByStatusMask_No_Fault_Default_Phy(self,Driver,req,resp):
          iStep = 1
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 1, iStep, False)
          time.sleep(2)
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
          with assume: assert uds_resp[5] < 4
 


     ids = ["Request {0}".format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in SID_0x19_reportNumberOfDTCByStatusMask]
     @allure.story('Test reportNumberOfDTCByStatusMask Test')
     @allure.severity('normal')
     @allure.story('Test ReadDTCInfomation Test')
     @allure.description('Test reportDTCByStatusMask of 0x19 in extended session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
     @pytest.mark.parametrize('req,resp',SID_0x19_reportNumberOfDTCByStatusMask,ids = ids)
     def test_reportNumberOfDTCByStatusMask_No_Fault_Extended_Phy(self,Driver,req,resp):
          iStep = 1
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 3, iStep, False)
          time.sleep(1)
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
          with assume: assert uds_resp[5] < 4

     @pytest.mark.smoke
     @allure.description('Test Report Number Of DTC By StatusMask with no fault in default session with physical addressing')
     @allure.testcase('Https://chengtech.pingcode.com/testhub/libraries/627628a182973e6c3aa153a4/cases')
     def test_Report_NumberOfDTCByStatusMask_Default_Phy(self,action,Driver):
          iStep = 1
          # check 默认状态下有几个故障
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x0B], [0x59,0x02,0x09], 1, iStep, False)
          DTC_data = uds_resp
          actuRespStr = binascii.b2a_hex(bytearray(DTC_data), ' ') 
          logging.info('DTC_data:{}'.format(actuRespStr))

          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x01,0x01], [0x59,0x01,0x09,0x00,0x00], 1, iStep, False)
          num = Driver['CAN'].reportNumberOfDTCByStatusMask(DTC_data,1)
          if  uds_resp[5] != num:
               logging.error(f'correct response:[{num}], actual response:[{uds_resp[5]}]')
          with assume: assert uds_resp[5] == num

     @pytest.mark.smoke
     @allure.description('Test Report Number Of DTC By StatusMask with no fault in extended session with physical addressing')
     @allure.testcase('Https://chengtech.pingcode.com/testhub/libraries/627628a182973e6c3aa153a4/cases')
     def test_Report_NumberOfDTCByStatusMask_Extended_Phy(self,action,Driver):
          iStep = 1
          # check 默认状态下有几个故障
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x0B], [0x59,0x02,0x09], 3, iStep, False)
          DTC_data = uds_resp
          actuRespStr = binascii.b2a_hex(bytearray(DTC_data), ' ') 
          logging.info('DTC_data:{}'.format(actuRespStr))

          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x01,0x01], [0x59,0x01,0x09,0x00,0x00], 1, iStep, False)
          num = Driver['CAN'].reportNumberOfDTCByStatusMask(DTC_data,1)
          if  uds_resp[5] != num:
               logging.error(f'correct response:[{num}], actual response:[{uds_resp[5]}]')
          with assume: assert uds_resp[5] == num


     @allure.severity('normal')
     @allure.description('Test reportNumberOfDTCByStatusMask of 0x19 when faults occur in default session with physical addressing')
     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
     def test_reportNumberOfDTCByStatusMask_Have_Fault_Default_Phy(self,action,Driver): 
          iStep = 1
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 1, iStep, False)
          self.testFaultNum(Driver, iStep)

     
     @allure.severity('normal')
     @allure.description('Test reportNumberOfDTCByStatusMask of 0x19 when faults occur in extended session with physical addressing')
     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
     def test_reportNumberOfDTCByStatusMask_Have_Fault_Extended_Phy(self,action,Driver): 
          iStep = 1
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 3, iStep, False)
          self.testFaultNum(Driver, iStep)
     


     def testFaultNum(self,Driver, iStep = 2):
          comm = Modbus(Set_up.NM_modbusCOM)
          # check 默认状态下有几个故障
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x0B], [0x59,0x02,0x09], 0, iStep, False)
          DTC_data = uds_resp
          actuRespStr = binascii.b2a_hex(bytearray(DTC_data), ' ') 
          logging.info('DTC_data:{}'.format(actuRespStr))

          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x01,0x01], [0x59,0x01,0x09,0x00,0x00], 0, iStep, False)
          num = Driver['CAN'].reportNumberOfDTCByStatusMask(DTC_data,1)
          if  uds_resp[5] != num:
               logging.error(f'correct response:[{num}], actual response:[{uds_resp[5]}]')
          with assume: assert uds_resp[5] == num

          with allure.step("Step4:Create fault of message 0x4A0 lost"):
               Driver['CAN'].set_0x2F1_lost_error(True)
               time.sleep(1)
          
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x0B], [0x59,0x02,0x09], 0, iStep, False)
          DTC_data = uds_resp
          actuRespStr = binascii.b2a_hex(bytearray(DTC_data), ' ') 
          logging.info('DTC_data:{}'.format(actuRespStr))

          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x01,0x01], [0x59,0x01,0x09,0x00,0x00], 0, iStep, False)
          num = Driver['CAN'].reportNumberOfDTCByStatusMask(DTC_data,1)
          if  uds_resp[5] != num:
               logging.error(f'correct response:[{num}], actual response:[{uds_resp[5]}]')
          with assume: assert uds_resp[5] == num
          
          with allure.step("Step7:Set voltage = 17V"):
               Driver['power'].set_vltage(17)
          time.sleep(1.5)
          
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x0B], [0x59,0x02,0x09], 0, iStep, False)
          DTC_data = uds_resp
          actuRespStr = binascii.b2a_hex(bytearray(DTC_data), ' ') 
          logging.info('DTC_data:{}'.format(actuRespStr))

          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x01,0x01], [0x59,0x01,0x09,0x00,0x00], 0, iStep, False)
          num = Driver['CAN'].reportNumberOfDTCByStatusMask(DTC_data,1)
          if  uds_resp[5] != num:
               logging.error(f'correct response:[{num}], actual response:[{uds_resp[5]}]')
          with assume: assert uds_resp[5] == num
          
          with allure.step("Step10:VCANH connects to VCANL"):
               comm.write_single_coil(DO_Address_single.Y7.value,0x0001)
               time.sleep(6)
          with allure.step("Step11:Remove fault of VCANH connnects to VCANL"):
               comm.write_single_coil(DO_Address_single.Y7.value,0x0000)
               time.sleep(1)
          logging.info('set VCANH connects to VCANL,wait 6s, then disconnect VCANH connects to VCANL')
          
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x0B], [0x59,0x02,0x09], 0, iStep, False)
          DTC_data = uds_resp
          actuRespStr = binascii.b2a_hex(bytearray(DTC_data), ' ') 
          logging.info('DTC_data:{}'.format(actuRespStr))

          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x01,0x01], [0x59,0x01,0x09,0x00,0x00], 0, iStep, False)
          num = Driver['CAN'].reportNumberOfDTCByStatusMask(DTC_data,1)
          if  uds_resp[5] != num:
               logging.error(f'correct response:[{num}], actual response:[{uds_resp[5]}]')
          with assume: assert uds_resp[5] == num
          
          time.sleep(5)
          
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x0B], [0x59,0x02,0x09], 0, iStep, False)
          DTC_data = uds_resp
          actuRespStr = binascii.b2a_hex(bytearray(DTC_data), ' ') 
          logging.info('DTC_data:{}'.format(actuRespStr))

          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x01,0x01], [0x59,0x01,0x09,0x00,0x00], 0, iStep, False)
          num = Driver['CAN'].reportNumberOfDTCByStatusMask(DTC_data,1)
          if  uds_resp[5] != num:
               logging.error(f'correct response:[{num}], actual response:[{uds_resp[5]}]')
          with assume: assert uds_resp[5] == num
          
          with allure.step("Step16:Set voltage = 12V"):
               Driver['power'].set_vltage(12)
          time.sleep(1.5)
     
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x0B], [0x59,0x02,0x09], 0, iStep, False)
          DTC_data = uds_resp
          actuRespStr = binascii.b2a_hex(bytearray(DTC_data), ' ') 
          logging.info('DTC_data:{}'.format(actuRespStr))

          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x01,0x01], [0x59,0x01,0x09,0x00,0x00], 0, iStep, False)
          num = Driver['CAN'].reportNumberOfDTCByStatusMask(DTC_data,1)
          if  uds_resp[5] != num:
               logging.error(f'correct response:[{num}], actual response:[{uds_resp[5]}]')
          with assume: assert uds_resp[5] == num

          with allure.step("Step19:Remove fault of message 0x4A0 lost"):
            Driver['CAN'].set_0x2F1_lost_error(False)
          time.sleep(1)
          
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x0B], [0x59,0x02,0x09], 0, iStep, False)
          DTC_data = uds_resp
          actuRespStr = binascii.b2a_hex(bytearray(DTC_data), ' ') 
          logging.info('DTC_data:{}'.format(actuRespStr))

          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x01,0x01], [0x59,0x01,0x09,0x00,0x00], 0, iStep, False)
          num = Driver['CAN'].reportNumberOfDTCByStatusMask(DTC_data,1)
          if  uds_resp[5] != num:
               logging.error(f'correct response:[{num}], actual response:[{uds_resp[5]}]')
          with assume: assert uds_resp[5] == num
          
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 0, iStep, False)

          return iStep
